#include "missle.h"
#include "commonFunc.h"

GLuint missle::tex;

using std::list;

missle::missle()
{
    //please NEVER call this
}
missle::missle(int nx, int ny, int nspeed, int nwidth, int nheight)
{
    //tex = ntex;
    ang = 0;
    speed = nspeed;
    x = nx;
    y = ny;
    width = nwidth;
    height = nheight;
    target = NULL;
    dmg = 3;
    timeSinceParticle = 0;
    maxTimeSinceParticle = 0.05;
}
missle::~missle()
{

}

//Graphics
void missle::draw()
{
    glPushMatrix();
    if (ang == 0)
    {
        glTranslatef(x,y,0);
    } else {
        glTranslatef(x+(width/2),y+(height/2),0);
        glRotatef(ang,0,0,1);
        glTranslatef(-(width/2),-(height/2),0);
    }
    
    glBindTexture(GL_TEXTURE_2D, gettex());
    glBegin(GL_QUADS);
    glTexCoord2i(0,0); //Texture coord
    glVertex3f(0,0,0); //Point coord

    glTexCoord2i(1,0);
    glVertex3f(width,0,0);
    
    glTexCoord2i(1,1);
    glVertex3f(width,height,0);

    glTexCoord2i(0,1);
    glVertex3f(0,height,0);
    glEnd();
    glPopMatrix();

    glLoadIdentity();
}



bool missle::move(double per, ptrContainer & dcontainer)
{
    //std::cout << per << std::endl;
    if (timeSinceParticle > 0)
        timeSinceParticle -= per;
    if (timeSinceParticle <= 0)
    {
        dcontainer.particlelist->push_back(new particle(dcontainer.particleTex[0], 2, 0.5, x, y,16,16,0,1,0,-10, 3, 3, 90));
        timeSinceParticle += maxTimeSinceParticle;
    }
    //get distance to target
    double curdist = getDist(x,y,target->x,target->y);
    //if the distance is withing the bounds of the enemy return true so missle can explode
    if (curdist < target->width)
    {
        dcontainer.particlelist->push_back(new particle(dcontainer.particleTex[1], 3, 0, target->x, target->y,32,32,0,0.5,0,0, 3, 3, 90));
        return true;
    }

    //multiply decimal percentage with the speed to get distance to travel
    //NOTE add consideration for angle later
    double dx = ((double)speed)*per;
    double dy = ((double)speed)*per;
    if (target->x > x)
    {
        x+=dx;
    } else {
        x-=dx;
    }

    if (target->y > y)
    {
        y+=dy;
    } else {
        y-=dy;
    }



    return false;
}


GLuint missle::gettex()
{
    return tex;
}
